﻿var postData = '';
var checkboxIndex = -1;
var forumName = '';
var threadName = '';
var interval = 7000;
var show = true;
var timer = setTimeout("dataChanged()", interval);
var pageType = "Posts";
//public string addPost(string username, string forumName, string threadName, string header, string body)
window.onload = function () {
    document.getElementById("PostBody").style.display = "none";
    var index = document.URL.indexOf(1);
    threadName = document.URL.split("Topic=")[1].split("&Thread=")[1];
    threadName = replaceCharacters(threadName, "%20", " ");
    forumName = document.URL.split("Topic=")[1].split("&Thread=")[0];
    
    getPosts(true);
}
// <![CDATA[
function dataChanged() {
    var username = getUsername();
    WebService.dataChanged(username, pageType, getPosts);
}

function getPosts(dataHasChanged) {
    if (dataHasChanged == true) {
        //use etai's code to parse url to retrieve query string
        WebService.getSPosts(forumName, threadName, onSuccessPosts, failOnGetPosts);
    }
    timer = setTimeout("dataChanged()", interval);
}

function failOnGetPosts(result) {
    alert(result);
}
function onSuccessPosts(result) {
    //change posts table according to result
    //alert(result[0].Body);
    //timer = setTimeout("getPosts()", 30000);
    postData = result;
    createPostTable();
}

// ]]>
function createPostTable() {
    var table = document.getElementById("postTable");
    for (var i = table.rows.length; i > 1; i--) {
        table.deleteRow(i - 1);
    }
    for (var i = 0; i < postData.length; i++) {
        addRowToPostTable(postData[i]);
    }
}

function showPost(header) {
    if (show == false) {
        for (var i = 0; i < postData.length; i++) {
            if (header == postData[i].Header) {
                var postBody = document.getElementById("PostBody");
                postBody.value = postData[i].Body;
                postBody.style.display = "";
            }
        }
        show = true;
    }
    else {
        document.getElementById("PostBody").style.display = "none";
        show = false;
    }
    
}
function addRowToPostTable(post) {
    var table = document.getElementById("postTable");

    var rowCount = table.rows.length;
    var row = table.insertRow(rowCount);

    var checkBox = row.insertCell(0);
    var element1 = document.createElement("input");
    element1.type = "checkbox";
    checkBox.appendChild(element1);

    var header = row.insertCell(1);
    var element1 = document.createElement("a");
    element1.setAttribute("onclick", 'showPost(\"' + post.Header.toString() + '\")');
    element1.onmouseover = function () {
        this.style.textDecoration = "underline";
        this.style.fontWeight = "bold";
    };
    element1.onmouseout = function () {
        this.style.textDecoration = "none";
        this.style.fontWeight = "normal";
    };
    var linkText = document.createTextNode(post.Header);
    //linkText.setAttribute("underli")
    element1.appendChild(linkText);
    header.appendChild(element1);

    //var header = row.insertCell(1);
    //header.innerHTML = post.Header;

    var PublishDate = row.insertCell(2);
    PublishDate.innerHTML = post.PublishDate;

    var Creator = row.insertCell(3);
    Creator.innerHTML = post.Creator;
}
function getSelectedItemsCount() {
    var count = 0;
    var table = document.getElementById("postTable");
    for (var i = 0; i < table.rows.length; i++) {
        var row = table.rows[i];
        var chkbox = row.cells[0].childNodes[0];
        if (null != chkbox && true == chkbox.checked) {
            checkboxIndex = i - 1;
            count++;
        }
    }
    return count;
}
function checkIfChoosePosts() {
    var numOfIndexes = getSelectedItemsCount();
    if (numOfIndexes == 0) {
        var error = document.getElementById("MainContent_CheckBoxErrorLabel");
        error.innerHTML = "Please Choose post";
        error.style.display = "";
        return false;
    }
    else if (numOfIndexes > 1) {
        var error = document.getElementById("MainContent_CheckBoxErrorLabel");
        error.innerHTML = "You chose more than one post";
        error.style.display = "";
        return false;
    }
    return true;
}
function showAddPostClicked()
{
    showElements(true);
    document.getElementById("AddPostButton").style.display = "";
}

function checkIfHavePermission(username, isModerator) {
    if (postData[checkboxIndex].Creator != username && isModerator == "False") {
        alert("Cannot edit post that you didn't create");
        return false;
    }
    return true;
}

function showEditPostClicked(username, isModerator) {
    if (checkIfChoosePosts() == true && checkIfHavePermission(username, isModerator) && checkIfMainPost() == false) {
        if (username != postData[checkboxIndex].Creator) {
            alert("Cannot edit post that you didn't create");
            return;
        }
        showElements(true);
        //document.getElementById("ShowEditPostButton").style.display = "";
        document.getElementById("MainContent_AddPostHeaderTextBox").value = postData[checkboxIndex].Header;
        document.getElementById("MainContent_AddPostBodyTextBox").value = postData[checkboxIndex].Body;
        document.getElementById("EditPostButton").style.display = "";
    }

}
function checkIfMainPost() {
    if(postData[checkboxIndex].Header == threadName) {
        alert("Cannot remove/edit main post, try remove/edit the thread");
        return true;
    }
    return false;
}

function deletePostClicked(username) {
    if (checkIfChoosePosts() == true) {
        if (checkIfMainPost() == false) {
            WebService.DeletePost(username, forumName, threadName, postData[checkboxIndex].Header, deleteSuccess, deleteFailed);
            document.getElementById("PostBody").value = "";
        }
        document.getElementById("MainContent_CheckBoxErrorLabel").style.display = "none";
    }
}
function deleteSuccess(result) {
    getPosts(true);
    alert(result);
}
function deleteFailed(result) {
    alert(result);
}
function cancelClick() {
    showElements(false);
}

function addEditButtonClick(username, isAdd) {

    var tNewHeader = document.getElementById("MainContent_AddPostHeaderTextBox").value;
    var tNewBody = document.getElementById("MainContent_AddPostBodyTextBox").value;
    if (checkParams(tNewHeader, tNewBody) == false ) {
        return; //Can't edit wrong parmas.
    }
    showElements(false);
    if (isAdd) {
        WebService.AddPost(username, forumName, threadName, tNewHeader, tNewBody, AddEditOnSuccess, AddEditOnFail);
    }
    else {
        WebService.EditPost(username, forumName, threadName,postData[checkboxIndex].Header, tNewHeader, tNewBody, AddEditOnSuccess, AddEditOnFail);
    }
    
}

function AddEditOnSuccess(result) {
    getPosts(true);
    alert(result);
    
}
function AddEditOnFail(result) {
    alert(result);
}
function checkParams(tNewHeader, tNewBody) {
    var tAns = true;
    var errorLabel = document.getElementById("MainContent_ErrorLabel");
    if (!(tNewHeader.length)) {
        errorLabel.innerHTML = "Please enter Post Header!";
        errorLabel.style.display = "inline";
        //document.getElementById("MainContent_AddPostErrorLabel").value = "Please enter Post Header!";
        //document.getElementById("MainContent_AddPostErrorLabel").style.display = "";//show it
        tAns = false;
    }
    else if (!(tNewBody.length)) {
        errorLabel.innerHTML = "Please enter Post Body!";
        errorLabel.style.display = "inline";
        //document.getElementById("MainContent_AddPostErrorLabel").value = "Please enter Post Body!";
        //document.getElementById("MainContent_AddPostErrorLabel").style.display = "";//show it
        tAns = false;
    }
    return tAns;
}

function showElements(show) {
    if (show) {
        document.getElementById("MainContent_AddPostHeaderLabel").style.display = "";
        document.getElementById("MainContent_AddPostHeaderTextBox").value = "";
        document.getElementById("MainContent_AddPostHeaderTextBox").style.display = "";
        document.getElementById("MainContent_AddPostBodyLabel").style.display = "";
        document.getElementById("MainContent_AddPostBodyTextBox").value = "";
        document.getElementById("MainContent_AddPostBodyTextBox").style.display = "";
        document.getElementById("CancelButton").style.display = "";
        document.getElementById("ShowAddPostButton").style.display = "none";
        document.getElementById("ShowEditPostButton").style.display = "none";
        document.getElementById("DeletePostButton").style.display = "none";
        document.getElementById("PostBody").style.display = "none";
    }
    else {
        document.getElementById("MainContent_AddPostHeaderLabel").style.display = "none";
        document.getElementById("MainContent_AddPostHeaderTextBox").value = "";
        document.getElementById("MainContent_AddPostHeaderTextBox").style.display = "none";
        document.getElementById("MainContent_AddPostBodyLabel").style.display = "none";
        document.getElementById("MainContent_AddPostBodyTextBox").value = "";
        document.getElementById("MainContent_AddPostBodyTextBox").style.display = "none";

        document.getElementById("ShowAddPostButton").style.display = "";
        document.getElementById("ShowEditPostButton").style.display = "";
        document.getElementById("DeletePostButton").style.display = "";

        document.getElementById("AddPostButton").style.display = "none";
        document.getElementById("EditPostButton").style.display = "none";
        document.getElementById("CancelButton").style.display = "none";

        document.getElementById("PostBody").value = "";
    }
    document.getElementById("MainContent_CheckBoxErrorLabel").style.display = "none";
}
function replaceCharacters(origString, inChar, outChar) {

    var newString = origString.split(inChar);
    newString = newString.join(outChar);

    return newString;
}